K8s Command
新增 namespace
只有第一次需要
kubectl create namespace <namespace_name>
切叢集
kubectl config get-contexts
kubectl config set current-context MY-CONTEXT
切 namespace
kubectl config set-context --current --namespace=my-namespace
secret: 使用 google 服務時 Authorization(授權)會用到
kubectl create secret generic mall-sa-key --from-file service-account.json
kubectl get secret
kubectl get secret <credentials> -o yaml
建立Ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: <repo_name>
namespace: <repo_name>-prod
annotations:
# kubernetes.io/ingress.class: nginx
#ingress.kubernetes.io/force-hsts: "true"
#ingress.kubernetes.io/hsts-preload: "true"
#ingress.kubernetes.io/hsts-max-age: "315360000"
#ingress.kubernetes.io/hsts-include-subdomains: "true"
# ingress.kubernetes.io/whitelist-source-range: "61.221.49.115/32, 61.221.49.116/32"
spec:
rules:
- host: your.domain.com
http:
paths:
- path: /<repo_name>
backend:
serviceName: <repo_name>-service
servicePort: http
---
然後想辦法推上去
kubectl apply -f kubernetes/ingress-prod.yaml
建立Pod
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: <repo_name>-deployment
name: <repo_name>-deployment
namespace: <repo_name>-prod
spec:
replicas: 1
selector:
matchLabels:
app: <repo_name>-deployment
template:
metadata:
labels:
app: <repo_name>-deployment
spec:
containers:
- name: <repo_name>-service
image: gcr.io/<project_name>/<repo_name>-prod
ports:
- name: http
containerPort: 80
imagePullPolicy: Always
readinessProbe:
httpGet:
path: /<repo_name>/
port: 80
initialDelaySeconds: 50
timeoutSeconds: 4
periodSeconds: 10
failureThreshold: 5
volumeMounts:
- name: timezone-config
mountPath: /etc/localtime
volumes:
- name: timezone-config
hostPath:
path: /usr/share/zoneinfo/Asia/Taipei
---
apiVersion: v1
kind: Service
metadata:
labels:
app: <repo_name>-service
name: <repo_name>-service
namespace: <repo_name>-prod
annotations:
traefik.backend.circuitbreaker: "NetworkErrorRatio() > 0.5"
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: <repo_name>-deployment
然後想辦法推上去
kubectl apply -f kubernetes/deployment-prod.yaml
shell script example(frontend)
project="xxx"
version="$1"
env="prod"
branch="prod"
if [ "$version" = "" ]
then
echo "version can't be empty"
exit
fi
open -a docker
#包版
echo "step 1. npm run build-$env"
npm run build-$env
if [[ "$?" -ne "0" ]]
then
echo "error"
exit 1
fi
# 退回上一層
cd ..
if [[ "$?" -ne "0" ]]
then
echo "error"
exit 1
fi
#建立docker image
echo "step 2. docker build -t gcr.io/<project_name>/$project-$env:latest -t gcr.io/<project_name>/$project-$env:$version -f docker/Dockerfile ."
docker build -t gcr.io/<project_name>/$project-$env:latest -t gcr.io/<project_name>/$project-$env:$version -f docker/Dockerfile .
if [[ "$?" -ne "0" ]]
then
echo "error"
exit 1
fi
# push image to gcp
echo "step 3. docker push gcr.io/<project_name>/$project-$env:$version"
docker push gcr.io/<project_name>/$project-$env:$version
if [[ "$?" -ne "0" ]]
then
echo "error"
exit 1
fi
echo "step 4. docker push gcr.io/<project_name>/$project-$env:latest"
docker push gcr.io/<project_name>/$project-$env:latest
if [[ "$?" -ne "0" ]]
then
echo "error"
exit 1
fi
echo "step 5. gcloud container clusters get-credentials k8s-cluster-prod --zone asia-east1-c --project <project_name>"
gcloud container clusters get-credentials k8s-cluster-prod --zone asia-east1-c --project <project_name>
if [[ "$?" -ne "0" ]]
then
echo "error"
exit 1
fi
# 更新kubernetes 鏡像
echo "step 5. kubectl -n $project-$env set image deployment/$project-deployment $project-service=gcr.io/<project_name>/$project-$env:$version"
kubectl -n $project-$env set image deployment/$project-deployment $project-service=gcr.io/<project_name>/$project-$env:$version
if [[ "$?" -ne "0" ]]
then
echo "error"
exit 1
fi
kubernetes常用命令
查询名命空间,namespaces or ns
kubectl get ns
----------------------------------------------------------------------
NAME STATUS AGE
default Active 18h
kube-public Active 18h
kube-system Active 18h
----------------------------------------------------------------------
建立namespaces
kubectl create namespace bagua-buddha-server-prod
刪除deployment
kubectl delete -f 你的deployment.yaml
查询pod
kubectl get pods --namespace {你的namespace}
kubectl get pods --all-namespaces
----------------------------------------------------------------------
NAME READY STATUS RESTARTS AGE
kube-apiserver-master1 1/1 Running 0 18h
kube-apiserver-master2 1/1 Running 0 17h
kube-apiserver-master3 1/1 Running 0 17h
----------------------------------------------------------------------
部署deployment
kubectl apply -f 你的deployment.yaml
查询对象详细信息
kubectl describe pods -n mgt-server-prod
查询日志
kubectl logs -f bagua-buddha-server-deployment-85fd786b9c-4bq5z --namespace=bagua-buddha-server-prod
-n 參數為區分環境用
stage: twpay-stage
production: twpay-prod
# 佈署/上版
kubectl delete -f kubernetes/deployment-all-stage.yaml
kubectl apply -f kubernetes/deployment-all-prod.yaml
# 看執行狀態
kubectl get pods -n mgt-server-prod
NAME READY STATUS RESTARTS AGE
twpay-deployment-7fdfdd6858-fxjgn 1/1 Running 0 97d
twpay-deployment-7fdfdd6858-txclt 1/1 Running 0 97d
# 看log
kubectl logs edge-server-deployment-864fbc4f78-kz76b -n edge-server-stage
#看起在哪個node
kubectl get pods -o wide -n {你的namespace}
#查起在哪個node
kubectl get pod --all-namespaces -o wide
#查目前node上的標籤
kubectl get nodes --show-labels
#增加標籤
kubectl label node gke-k8s-cluster-default-pool-9816fadf-h5w4 pv=true
kubectl label node gke-k8s-cluster-default-pool-9816fadf-6srq pv-
#強制刪除pod
kubectl delete po redis-app-0 -n redis-cluster-prod --force --grace-period=0
kubectl exec -ti fubon-deployment-84fd6db95c-6kx2j -n fubon-prod -- /bin/sh
↓namespace
kubectl -n bagua-buddha-server-prod set image deployment/bagua-buddha-server-deployment bagua-buddha-server-service=gcr.io/<project_name>/bagua-buddha-server-prod:1.9 <-版本號
kubectl -n bagua-buddha-server-stage set image deployment/bagua-buddha-server-deployment bagua-buddha-server-service=gcr.io/<project_name>/bagua-buddha-server-stage:2.3